<template>
  <div class="m-login-container">
    <el-row class="m-main-header-search" justify="space-between">
      <el-form
        hide-required-asterisk
        es="rules"
        :inline="true"
        ref="refForm"
        justify="space-between"
      >
        <el-form-item prop="fz_value" label="模糊搜索">
          <el-input
            clearable
            size="small"
            autofocus
            v-model="refFormData.fz_value"
            placeholder="商品信息、用户信息"
          ></el-input>
        </el-form-item>
        <el-form-item prop="status" label="审核状态">
          <el-select
            v-model="refFormData.status"
            size="small"
            filterable
            reserve-keyword
            placeholder="状态"
          >
            <el-option
              v-for="item in orderStatusList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="检索时间">
          <el-date-picker
            v-model="time"
            type="daterange"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            size="small"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleSearch()" type="primary" size="small">
            查询
          </el-button>
        </el-form-item>
      </el-form>
      <el-button @click="handleExport()" plain size="small" icon="el-icon-download">
        导出
      </el-button>
    </el-row>

    <div class="m-main-content-body">
      <p class="contant-body-title">列表</p>
      <el-table
        v-loading="loading"
        :data="list"
        tooltip-effect="dark"
        class="table"
        stripe
        :inline="false"
        :header-cell-style="{
          'background-color': '#E5EEFF',
        }"
      >
        <el-table-column
          prop="order_sn"
          label="订单编号"
          :autoFit="true"
          :fitHeader="true"
        >
        </el-table-column>
        <el-table-column
          prop="goods_sn"
          label="商品编号"
          :autoFit="true"
          :fitHeader="true"
        ></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名"
          :autoFit="true"
          :fitHeader="true"
        ></el-table-column>
        <el-table-column
          prop="goods_region"
          label="商品地址"
          :autoFit="true"
          :fitHeader="true"
        ></el-table-column>
        <el-table-column
          prop="phone"
          label="手机号"
          :autoFit="true"
          :fitHeader="true"
        ></el-table-column>
        <el-table-column
          prop="user_name"
          label="用户名"
          :autoFit="true"
          :fitHeader="true"
        ></el-table-column>
        <el-table-column
          prop="price"
          label="cod价格"
          :autoFit="true"
          :fitHeader="true"
        ></el-table-column>
        <el-table-column prop="audit" label="审核状态">
          <template slot-scope="scope">
            <el-tag
              size="small"
              effect="dark"
              :color="getTagName(scope.row.audit, 'color')"
              >{{ getTagName(scope.row.audit) }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="地址"
          :autoFit="true"
          :fitHeader="true"
        ></el-table-column>
        <el-table-column
          prop="ip"
          label="ip"
          :autoFit="true"
          :fitHeader="true"
        ></el-table-column>
        <el-table-column
          prop="ip_region"
          label="ip归属地"
          :autoFit="true"
          :fitHeader="true"
        ></el-table-column>
        <el-table-column
          prop="remarks"
          label="备注"
          :autoFit="true"
          :fitHeader="true"
        ></el-table-column>
        <el-table-column
          prop="biz_user_name"
          label="业务员名称"
          :autoFit="true"
          :fitHeader="true"
        ></el-table-column>
        <el-table-column
          prop="express_status"
          label="物流状态"
          :autoFit="true"
          :fitHeader="true"
        >
          <template slot-scope="scope">
            {{ scope.row.express_status | expressStatusName }}
          </template>
        </el-table-column>
        <el-table-column
          prop="express_no"
          label="物流单号"
          :autoFit="true"
          :fitHeader="true"
        >
        </el-table-column>
        <el-table-column
          prop="time"
          label="下单时间"
          :autoFit="true"
          :fitHeader="true"
        >
          <template slot-scope="scope">
            {{
              scope.row.time
                ? $moment(scope.row.time).format("YYYY/MM/DD HH:mm:ss")
                : "--"
            }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          :autoFit="true"
          :fitHeader="true"
        >
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.audit === 1"
              class="table-operate-btn"
              @click="handleOperateExpress(scope.row)"
              type="text"
              size="small"
            >
              {{ scope.row.express_status === 1 ? "取消出库" : "一键出库" }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="(val) => handlePageMetaChange(val, 'size')"
          @current-change="(val) => handlePageMetaChange(val, 'page')"
          :current-page="pageMeta.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageMeta.limit"
          layout="sizes, prev, next, jumper"
          :total="pageMeta.total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { auditList, getTagName } from "../auditList/config/index";
import { orderStatusList, getExpressStatusName } from "./config/index";

export default {
  components: {},
  filters: {
    expressStatusName: getExpressStatusName,
  },
  data() {
    return {
      orderStatusList,
      list: [],
      loading: false,
      dislogSetVisible: false,
      refFormData: {
        status: null,
      },
      pageMeta: {
        page: 1,
        limit: 10,
        total: 1e10,
      },
      time: [
        this.$moment(new Date()).format("YYYY-MM-DD"),
        this.$moment(new Date()).format("YYYY-MM-DD"),
      ],
      auditList,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getTagName,
    /*
     * @Desc: 获取列表
     * @params: {String}
     * @return: {Object}
     */
    async getList() {
      const [start_time, end_time] = this.time;
      this.loading = true;
      const { data } = await this.$api.order.getList({
        ...this.pageMeta,
        page: this.pageMeta.page - 1,
        ...this.refFormData,
        start_time: start_time + " 00:00:00",
        end_time: end_time + " 23:59:59",
      });
      const { code, data: list } = data;
      if (code === 200) {
        this.list = list;
      }
      this.loading = false;
    },

    /*
     * @Desc:查询
     * @params: {String}
     * @return: {Object}
     */
    async handleSearch() {
      this.pageMeta.page = 1;
      this.getList();
    },

    /**
     * @Desc 分页
     * @params {String} val page/limit
     * @params {String} type  page/size
     * @return {Object}
     * */
    handlePageMetaChange(val, type = "size") {
      this.pageMeta[type === "size" ? "limit" : "page"] = val;
      this.getList();
    },

    /**
     * @Desc 物流操作
     * @return {Object}
     * */
    handleOperateExpress({ express_status, id }) {
      console.log(express_status);
      this.$delConfirm(
        null,
        express_status === 1 ? "确认取消出库" : "确认出库",
        async () => {
          const { data } = await this.$api.order.orderExpressOperate({
            [express_status === 1 ? "delete" : "create"]: { id },
          });
          const { code, msg } = data;
          if (code === 200) {
            this.$message.success("操作成功");
            this.getList();
          } else {
            this.$message.warning(msg);
          }
        }
      );
    },

    /**
     * @Desc 导出
     * */
    handleExport() {
      const [start_time, end_time] = this.time;
      // window.open(this.$api.order.export(start_time, end_time))
      const link = document.createElement("a");
      link.href = this.$api.order.export(
        start_time + " 00:00:00",
        end_time + " 23:59:59"
      );
      link.style.display = "none";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    },
  },
};
</script>

<style lang="less" scoped>
/deep/ .table {
  width: calc(100vw - 336px);
}
</style>
